
using System;

using PolePosition.v3.EntityLibrary;
namespace PolePosition.v3.EntityFactory
{
    /// <summary>
    /// 
    /// </summary>
	/// <remarks></remarks>
	public sealed partial class VendorSettingFactory 
	{		
		/// <summary>
		/// This static constructor is responsible for subscribing to all events in the class. 
		/// If Child Collections were created, then this constructor subscribes to events 
		/// that handle getting and saving child collections.
		/// </summary>
        /// <remarks><para>This constructor is present in VendorSettingFactory because VendorSetting Entities
        /// have Child Collection properties. Child Collection properties contain groups of additional Entities
        /// that in turn, contain data related to VendorSetting Entities. Whether Child Collections, this 
        /// constructor, and the event handlers it wires up are present was determined at the time this code was
        /// generated. See the documentation for the <see cref="Classes.Entities.VendorSettingEntity" /> for more.
        /// </para>
        /// <para>This constructor makes sure that all actions by the Factory that are relevant to Child Collections
        /// are responded to. This means subscribing to the events raised by any of the <c>Save()</c>, 
        /// <c>GetEntity()</c> and <c>GetCollection()</c> overloads (see the documentation for
        /// <see cref="Classes.Factories.VendorSettingFactory"/>). The event handlers assigned by this constructor
        /// ensure that Child Collections are properly retrieved, saved, or deleted (that is, preventing any concurrency
		/// violations) whenever the VendorSettingEntity they belong to is modified.</para>
        /// <para>This constructor assigns handlers to the following VendorSettingFactory
        /// events:</para>
        /// <list type="bullet">
            /// <item>"Get" events: <see cref="VendorSettingFactory.OnAfterGetEntity" /> and 
            /// <see cref="VendorSettingFactory.OnAfterGetEntityCollection" />. This ensures that when a 
            /// VendorSettingEntity is retrieved from the database, any Child Collections it needs are retrieved also.
            /// </item>
            /// 
            /// <item>"Save" events: <see cref="VendorSettingFactory.OnBeforeSaveEntityBegin" />, 
            /// <see cref="VendorSettingFactory.OnBeforeSaveCollectionBegin" />, and 
            /// <see cref="VendorSettingFactory.OnBeforeSaveEntityCommit" />. 
            /// Since transactions are used when saving Entities, subscription to the first two
            /// events ensures that only one transaction is started and that it is commited by the correct Factory 
            /// <c>Save()</c> method (see how the 
            /// <see cref="Classes.Factories.VendorSettingFactory.Save(Classes.Entities.VendorSettingEntity, bool)"/>
            /// method works).<para></para>
            /// Subscription to the third event ensures that when a VendorSettingEntity is saved, any Child Collections
            /// it contains are also saved and that they are saved in the correct order to avoid concurrency problems.
            /// </item>
            /// 
            /// <item>"Delete" events: <see cref="VendorSettingFactory.OnBeforeEntityDelete" />. This ensures that when
            /// a VendorSettingEntity is deleted, any Child Collections it contains are deleted first to avoid concurrency
			/// problems.
            /// </item>
        /// </list>
        /// <para>One event handler is assigned to each event for every Child Collection that needs to be present
        /// for a VendorSettingEntity. So, if the Entity has two Child Collections, two handlers are added to 
        /// each event; in that way, all collections will react when an event is raised. This constructor assigns
		/// handlers for:
		/// <list type="bullet">
        /// </list>
        /// </para>
		/// <para>Handler methods were generated for each event and for each collection type, and are also part
		/// of the VendorSettingFactory (they are all identified by a composed name that consists of: the
		/// event they respond to, the child collection they belong to, and the word "Handler" at the end.</para>
		/// <para>Note also that this constructor calls the 
		/// <see cref="Classes.Factories.VendorSettingFactory.CustomConstructor()"/> method at the end. Since
		/// code for that method is not generated by the code generation tool that generated this constructor, 
		/// calling that method allows for custom code to execute along with this constructor.</para>
        /// </remarks>
        static void ChildCollectionConstructor()
        {
			#region Wire Up Event Handling for Vendor Parent Entity
			//Get Methods - wire up the get events so that when the parent class is 
			//aquired the children are also
            VendorSettingFactory.OnAfterGetEntity += OnAfterGetVendorSettingEntityForVendorHandler;
                  
            //Entity Save
            VendorSettingFactory.OnBeforeSaveEntityBegin += VendorSettingFactory.OnBeforeSaveVendorSettingEntityForVendorBeginHandler;

         	#endregion
		
		}
		
		#region Event Handlers: Vendor 
		
		/// <summary>
        /// Stores the number that indicates the order of the Vendor table in the
		/// DataSet returned by the Factory's <c>Get()</c> methods. 
        /// </summary>
		/// <remarks>This number is used by the <see cref="OnAfterGetVendorEntityHandler" />
		/// and the <see cref="OnAfterGetVendorCollectionHandler" /> methods to determine
		/// if the Vendor table exists in the DataSet. This number was
		/// determined at the time the code was generated.
		/// </remarks>
		private const int VendorOrdinalPosition  = 1;
		
		#region Get Vendor Event Handlers

		/// <summary>

		/// </remarks>
        static void OnAfterGetVendorSettingEntityForVendorHandler(object sender, Classes.Bases.AfterGetEntityEventArgs args)
        {
			
            //ensure that the dataset contains the second datatable constaining
            //the Vendor data
            if (args.DataSetFromDataBase.Tables.Count < VendorOrdinalPosition  + 1)
            {
                ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataTableException("Vendor not available for addition to VendorSetting entity; The factory event handler found that there is no member ordinally placed in the dataset datatable collection."));
            }


            //redeclare and cast the argument result entity 
            VendorSettingEntity EntityToReturn;
            EntityToReturn = (VendorSettingEntity)args.EntityToReturn;

			//ensure the entity is created
			if (EntityToReturn == null)
				return;
			
			//  pull the first row from the correct table for the new Vendor entity
			EntityToReturn.VendorIdEntity =
			   new VendorEntity( args.DataSetFromDataBase.Tables[VendorOrdinalPosition].Rows[0], args.UseCheckSum);
  	      }		

        #endregion
		
		#region Insert/Update/Delete Vendor Event Handlers
		
        /// <summary>
		/// </remarks>
        static void OnBeforeSaveVendorSettingEntityForVendorBeginHandler(object sender, Classes.Bases.BeforeSaveEntityBeginEventArgs args)
        {
			//redeclare and cast the argument save entity
            VendorSettingEntity EntityToSave;
            EntityToSave = (VendorSettingEntity)args.EntityToSave;

			//make sure the child collection is actually used
			if (EntityToSave.VendorIdEntity == null || !(bool)EntityToSave.VendorIdEntity.IsDirty)
				return;
				
            // insert/update/delete all the child eities
            VendorFactory.Save(EntityToSave.VendorIdEntity, args.UseCheckSum);

			//set the foreign key value to the PK of the parent
			EntityToSave.VendorFK = EntityToSave.VendorIdEntity.VendorId;
        }

        #endregion
		
 		#endregion
		
	}
}